import { Component } from '@angular/core';

import { AlertController, App, ModalController, NavController} from 'ionic-angular';
import { ServiceData } from '../../providers/service-data';

@Component({
  templateUrl: 'build/pages/contacts/contacts.html',
  providers: [ServiceData]
})
export class ContactsPage {

  constructor(
    public alertCtrl: AlertController,
    public app: App,
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public serviceData: ServiceData
  ) {

  }

  ionViewDidEnter() {
    this.app.setTitle('人脉');
  }

  ngAfterViewInit() {
    this.loadItems();
  }


  items = [];
  url = 'https://www.reddit.com/r/Android/new/.json';
  /**
   * 加载列表信息
   */
  loadItems() {
    var params = {};
    if (this.items.length > 0) {
      params['after'] = this.items[this.items.length - 1].name;
    }
    this.serviceData.load(this.url, params).then(data => {
      data.data.children.forEach(item => {
        this.items.push(item);
      });
    });
  }

  /**
   * 上滑加载分页
   */
  doInfinite(infiniteScroll) {
    console.log('开始加载分页');
    setTimeout(() => {
      this.loadItems();
      infiniteScroll.complete();
    }, 500);
  }
  /**
   * 下滑刷新
   */
  doRefresh(refresher) {
    console.log('Begin async operation', refresher);

    setTimeout(() => {
      console.log('Async operation has ended');
      refresher.complete();
    }, 2000);
  }
  
}
